import { DataSource } from 'typeorm';
import { TaskComment } from '../../../entities/task-comment.entity';
import { Task } from '../../../entities/task.entity';
import { User } from '../../../entities/user.entity';

/**
 * 任务评论种子数据
 * 负责初始化示例任务评论
 */
export class TaskCommentSeed {
  constructor(private dataSource: DataSource) {}

  /**
   * 运行任务评论种子
   */
  async run(): Promise<void> {
    console.log('开始初始化任务评论数据...');

    const taskCommentRepository = this.dataSource.getRepository(TaskComment);
    const taskRepository = this.dataSource.getRepository(Task);
    const userRepository = this.dataSource.getRepository(User);

    // 查找所有用户
    const adminUser = await userRepository.findOneBy({ username: 'admin' });
    const normalUser = await userRepository.findOneBy({ username: 'user' });

    if (!adminUser || !normalUser) {
      console.log('未找到基础用户，跳过任务评论初始化');
      return;
    }

    // 查找所有任务
    const tasks = await taskRepository.find();

    if (tasks.length === 0) {
      console.log('未找到任务，跳过任务评论初始化');
      return;
    }

    // 为每个任务添加评论
    for (const task of tasks) {
      // 添加初始评论
      const initialComment = taskCommentRepository.create({
        content: `开始处理任务：${task.title}。将按照计划进行。`,
        taskId: task.id,
        userId: adminUser.id
      });
      await taskCommentRepository.save(initialComment);

      // 添加进度评论
      const progressComment = taskCommentRepository.create({
        content: `任务进展顺利，已完成初步工作。`,
        taskId: task.id,
        userId: normalUser.id
      });
      await taskCommentRepository.save(progressComment);

      // 为部分任务添加更多评论
      if (task.title.toLowerCase().includes('设计') || task.title.toLowerCase().includes('开发')) {
        const designComment = taskCommentRepository.create({
          content: `已完成设计稿，请查看附件。`,
          taskId: task.id,
          userId: adminUser.id
        });
        await taskCommentRepository.save(designComment);

        const feedbackComment = taskCommentRepository.create({
          content: `设计稿看起来不错，建议调整一下颜色方案。`,
          taskId: task.id,
          userId: normalUser.id
        });
        await taskCommentRepository.save(feedbackComment);
      }

      console.log(`为任务 "${task.title}" 添加了评论`);
    }

    console.log('任务评论数据初始化完成');
  }
} 